<template>
  <div class="login_container">
    <!-- 登录盒子开始 -->
    <div class="login_box">
      <!-- 头像logo开始 -->
      <div class="avator_img">
        <img src="../assets/images/logo.png" alt />
      </div>
      <!-- 头像logo结束 -->
      <!-- 登录表单开始 -->
      <el-form
        ref="LoginFormRef"
        label-width="0px"
        class="login_form"
        :model="loginform"
        :rules="LoginFormRules"
      >
        <!-- 用户名文本框 -->
        <el-form-item prop="username">
          <el-input placeholder="用户名" prefix-icon="el-icon-s-custom" v-model="loginform.username"></el-input>
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="密码"
            prefix-icon="el-icon-unlock"
            v-model="loginform.password"
          ></el-input>
        </el-form-item>
        <!-- 登录和重置按钮 -->
        <el-form-item class="loginandout">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 登录表单结束 -->
    </div>
    <!-- 登录盒子结束 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单数据
      loginform: {
        username: "admin",
        password: "123456"
      },
      // 表单验证规则
      LoginFormRules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 重置表单方法
    resetForm() {
      this.$refs.LoginFormRef.resetFields();
    },
    // 登录预验证，本地存储，路由跳转
    login() {
      this.$refs.LoginFormRef.validate(async value => {
        if (!value) return;
        const { data: res } = await this.$http.post("login", this.loginform);
        if (res.meta.status !== 200) return this.$message.error("登录失败");
        this.$message.success("登录成功");
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login_container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    .avator_img {
      position: absolute;
      left: 50%;
      width: 130px;
      height: 130px;
      padding: 10px;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      box-shadow: 0 0 10px #ddd;
      border: 1px solid #cccccc;
      background-color: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login_form {
      box-sizing: border-box;
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      .loginandout {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>>
